﻿using System;
using System.Security.Cryptography;
using System.Text;
using UtilitiesLib.Model;

namespace UtilitiesLib
{
    public static class Hasher
    {
        public static string GetHash(string text, HashType hashType)
        {
            string hashString;
            switch (hashType)
            {
                case HashType.MD5:
                    hashString = GetMD5(text);
                    break;

                case HashType.SHA1:
                    hashString = GetSHA1(text);
                    break;

                case HashType.SHA256:
                    hashString = GetSHA256(text);
                    break;

                case HashType.SHA512:
                    hashString = GetSHA512(text);
                    break;

                default:
                    throw new ArgumentException("Incorrect hash type", "hashType");
            }
            return hashString;
        }

        public static bool CheckHash(string original, string hashString, HashType hashType)
        {
            string originalHash = GetHash(original, hashType);
            return (originalHash == hashString);
        }

        #region Private

        private static string GetMD5(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] bytes = UE.GetBytes(text);

            MD5 md5 = new MD5CryptoServiceProvider();
            string hex = string.Empty;

            hashValue = md5.ComputeHash(bytes);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA1(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA1Managed sha1 = new SHA1Managed();
            string hex = "";

            hashValue = sha1.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA256(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA256Managed sha256 = new SHA256Managed();
            string hex = "";

            hashValue = sha256.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        private static string GetSHA512(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA512Managed sha512 = new SHA512Managed();
            string hex = "";

            hashValue = sha512.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        #endregion Private
    }
}